#include "t0022-0.h"
#include "dxtviewer.h"
#include "appwidget.h"
#include "cui.h"
#include <signal.h>

#define MODNAME     "[POPM]"
extern struct dxtviewer_st dv;
int popmenu_keyevent(struct cui_widget_st* widget, int key)
{
	int handled = CUI_WIDGETEVENT_PROPAGATION;

	ERRSYS_INFOPRINT("%s:%s\n", widget->name, __func__);
	switch (key) {
		case KEY_ENTER_US:
			key = KEY_ENTER;
		case KEY_ENTER:
		case KEY_ESC:
			handled = CUI_WIDGETEVENT_HANDLED;
			break;
	}

    if (KEY_ENTER == key) {
        struct cui_menu_st* cmenu;
        ITEM *item;
        int index = 0;
        struct appdata_st *appdata;
        struct l0032_xtable_entry *entry;
        struct l0032_xtable_namedpoint *namedpoint;
        struct l0032_xtable_valueenum *valueenum;
        cmenu = widget->widgetinstance;
        item = current_item(cmenu->menu);
        index = item_index(item);
        appdata = widget->priv;
        entry = appdata->entry;
        namedpoint = &entry->namedpoints[appdata->point_index];
        valueenum = &namedpoint->valueenum[index];

        ERRSYS_INFOPRINT("selected %d:%s\n", (int)valueenum->val, valueenum->valdesc);
        l0032_xtable_write(entry->hentry, appdata->point_index, &valueenum->val);
        key = KEY_ESC;  // written and quit
    }
    
    if (KEY_ESC == key) {
        cui_screen_pop(widget->cscr);
        cui_refresh(widget->cscr, CUI_CURSOR_INVISIBLE);
        ERRSYS_INFOPRINT("popmenu quit\n");
    }
	return handled;
}

int popmenu_load(struct appdata_st *appdata)
{
    int retval = -1;
    struct cui_widget_st *widget = get_widget_byname(&dv.cscr->windowlist, "popmenu");
    struct l0032_xtable_entry *entry = appdata->entry;
    struct l0032_xtable_namedpoint *namedpoint = &entry->namedpoints[appdata->point_index];

    if (widget) {
        int n = namedpoint->nvalues;
        const char **item_namelist = zmalloc(sizeof(char*) * n);
        char *buf = zmalloc(sizeof(char) * L0032_DXT_DESC_MAX_LEN * n);
        widget->priv = appdata;
        if (item_namelist && buf) {
            int i, j, val;
            struct cui_menu_st *cmenu = widget->widgetinstance;

            char title[CUI_WIDGETTITLE_BUFLEN] = {0};
            snprintf(title, CUI_WIDGETTITLE_BUFLEN, "Data Settings Point [%d]", appdata->point_index);
            cui_set_widget_title(widget, title);

            for (i = 0;i < n;i ++) {
                struct l0032_xtable_valueenum *valueenum = &namedpoint->valueenum[i];
                ERRSYS_INFOPRINT("%d:%s\n", (int)valueenum->val, valueenum->valdesc);
                snprintf(buf + i * L0032_DXT_DESC_MAX_LEN, L0032_DXT_DESC_MAX_LEN, "%d:%s", (int)valueenum->val, valueenum->valdesc);
                item_namelist[i] = buf + i * L0032_DXT_DESC_MAX_LEN;
            }
            cui_menu_initialize(widget, namedpoint->nvalues, item_namelist, NULL, -1);

            l0032_xtable_read(entry->hentry, appdata->point_index, &val, sizeof(int));
            ERRSYS_INFOPRINT("%s:[%d] read value:%d\n", __func__, appdata->point_index, val);
            for (j = 0;j < namedpoint->nvalues;j ++) {
                struct l0032_xtable_valueenum *valueenum = &namedpoint->valueenum[j];
                if (valueenum->val == val) {
                    set_current_item(cmenu->menu, cmenu->items[j]);
                    break;
                }
            }
            cui_widget_align(widget, CUI_WIDGET_ALIGNMENT_XYMID);
            cui_set_widget_eventslot(widget, CUI_EVENTCB_TYPE_KEYEVENT, popmenu_keyevent);
            cui_screen_push(widget, NULL);
    		cui_refresh(widget->cscr, -1);
        }
        free(buf);
        free(item_namelist);
    }

    return retval;
}